package com.tc.controller;

import com.github.pagehelper.PageInfo;
import com.tc.pojo.Trucks;
import com.tc.service.TrucksService;
import entity.Result;
import entity.StatusCode;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:admin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(value = "TrucksController")
@RestController
@RequestMapping("/trucks")
@CrossOrigin
public class TrucksController {

    @Autowired
    private TrucksService trucksService;

    /***
     * Trucks分页条件搜索实现
     * @param trucks
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "Trucks条件分页查询",notes = "分页条件查询Trucks方法详情",tags = {"TrucksController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) @ApiParam(name = "Trucks对象",value = "传入JSON数据",required = false) Trucks trucks, @PathVariable  int page, @PathVariable  int size){
        //调用TrucksService实现分页条件查询Trucks
        PageInfo<Trucks> pageInfo = trucksService.findPage(trucks, page, size);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * Trucks分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "Trucks分页查询",notes = "分页查询Trucks方法详情",tags = {"TrucksController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用TrucksService实现分页查询Trucks
        PageInfo<Trucks> pageInfo = trucksService.findPage(page, size);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param trucks
     * @return
     */
    @ApiOperation(value = "Trucks条件查询",notes = "条件查询Trucks方法详情",tags = {"TrucksController"})
    @PostMapping(value = "/search" )
    public Result<List<Trucks>> findList(@RequestBody(required = false) @ApiParam(name = "Trucks对象",value = "传入JSON数据",required = false) Trucks trucks){
        //调用TrucksService实现条件查询Trucks
        List<Trucks> list = trucksService.findList(trucks);
        return new Result<List<Trucks>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Trucks根据ID删除",notes = "根据ID删除Trucks方法详情",tags = {"TrucksController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用TrucksService实现根据主键删除
        trucksService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改Trucks数据
     * @param trucks
     * @param id
     * @return
     */
    @ApiOperation(value = "Trucks根据ID修改",notes = "根据ID修改Trucks方法详情",tags = {"TrucksController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @PutMapping(value="/{id}")
    public Result update(@RequestBody @ApiParam(name = "Trucks对象",value = "传入JSON数据",required = false) Trucks trucks, @PathVariable Integer id){
        //设置主键值
        trucks.setId(id);
        //调用TrucksService实现修改Trucks
        trucksService.update(trucks);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增Trucks数据
     * @param trucks
     * @return
     */
    @ApiOperation(value = "Trucks添加",notes = "添加Trucks方法详情",tags = {"TrucksController"})
    @PostMapping
    public Result add(@RequestBody  @ApiParam(name = "Trucks对象",value = "传入JSON数据",required = true) Trucks trucks){
        //调用TrucksService实现添加Trucks
        trucksService.add(trucks);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询Trucks数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Trucks根据ID查询",notes = "根据ID查询Trucks方法详情",tags = {"TrucksController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @GetMapping("/{id}")
    public Result<Trucks> findById(@PathVariable Integer id){
        //调用TrucksService实现根据主键查询Trucks
        Trucks trucks = trucksService.findById(id);
        return new Result<Trucks>(true,StatusCode.OK,"查询成功",trucks);
    }

    /***
     * 查询Trucks全部数据
     * @return
     */
    @ApiOperation(value = "查询所有Trucks",notes = "查询所Trucks有方法详情",tags = {"TrucksController"})
    @GetMapping
    public Result<List<Trucks>> findAll(){
        //调用TrucksService实现查询所有Trucks
        List<Trucks> list = trucksService.findAll();
        return new Result<List<Trucks>>(true, StatusCode.OK,"查询成功",list) ;
    }
}
